package com.auto.survey.widget.dialog.optionsdialog

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R


class OptionsAdapter(
    private val options: List<OptionItem>,
    private val onClick: (OptionItem) -> Unit
) : RecyclerView.Adapter<OptionsAdapter.OptionViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OptionViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_option_button, parent, false)
        return OptionViewHolder(view)
    }

    override fun onBindViewHolder(holder: OptionViewHolder, position: Int) {
        val option = options[position]
        holder.textView.text = option.text
        holder.textView.setOnClickListener { onClick(option) }

        // 最后一项不显示分隔线
        holder.divider.visibility = if (position == options.size - 1) {
            View.GONE
        } else {
            View.VISIBLE
        }

        // 可选：设置图标
        option.iconResId?.let {
            holder.textView.setCompoundDrawablesWithIntrinsicBounds(
                it, 0, 0, 0
            )
            holder.textView.compoundDrawablePadding = 16
        }
    }

    override fun getItemCount(): Int = options.size

    inner class OptionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.tv_option)
        val divider: View = itemView.findViewById(R.id.divider)
    }
}